namespace("mk");
mk.Application = {
    meID:null,
    me:null,
    players:{},
    preinit: function() {
         document.body.oncontextmenu = function(){
             return false;
         }
    },
    init: function() {
        this.preinit();
        var context = this;
        sg.Loader.addEventListener("onload", function() {
            context.run();
        });
        sg.Loader.loadImages(mk.images);
    },
    run: function() {
        var context = this;
        mk.SocketManager.addEventListener("connected", function(obj) {
            context.meID = obj.key;
            context.players[obj.key] = context.me = new mk.objects.chars.Char(300, 300, obj.key, mk.chars.char1);
            context.me.view.startAnimation();
            mk.controlManager.init(context.me);
        });
        mk.SocketManager.addEventListener("userJoined", function(obj) {
            if(!context.players[obj.key]) {
                context.players[obj.key] = new mk.objects.chars.Char(300, 300, obj.key);
                context.players[obj.key].view.startAnimation();
            }
        });
        mk.SocketManager.addEventListener("userSplit", function(obj) {
            if(context.players[obj.key]) {
                context.players[obj.key].destroy();
                context.players[obj.key] = null;
            }
        });
        mk.SocketManager.addEventListener("initPlayers", function(obj) {
            for(var i in obj.data) {
                context.players[i] = new mk.objects.chars.Char(obj.data[i].x, obj.data[i].y, i);
                context.players[i].view.startAnimation();
            }
        });
        mk.SocketManager.addEventListener("messageSent", function(obj) {
            switch (obj.action.type) {
                case "hand":
                context.me.setAngles(mk.controlManager.calculateAngles(context.me, obj.action.mx, obj.action.my, obj.action.lmb, obj.action.rmb))
                break;
                case "action":
                    context.me.action(obj.action.action);
                    context.me.moveKX = obj.action.moveKX != undefined ? obj.action.moveKX : context.me.moveKX;
                    context.me.moveKY = obj.action.moveKY != undefined  ? obj.action.moveKY : context.me.moveKY
                    break;
                case "direction":
                    context.me.direction = obj.action.direction
            }
        });
        mk.SocketManager.addEventListener("messageReceived", function(obj){
            if(context.players[obj.key])  {
                switch (obj.action.type) {
                    case "hand":
                        context.players[obj.key].setAngles(mk.controlManager.calculateAngles(context.players[obj.key], obj.action.mx, obj.action.my, obj.action.lmb, obj.action.rmb))
                        break;
                    case "action":
                        context.players[obj.key].action(obj.action.action);
                        context.players[obj.key].moveKX = obj.action.moveKX != undefined ? obj.action.moveKX : context.players[obj.key].moveKX;
                        context.players[obj.key].moveKY = obj.action.moveKY != undefined  ? obj.action.moveKY : context.players[obj.key].moveKY;
                        context.players[obj.key].direction = obj.action.direction || 1;
                        context.players[obj.key].x = obj.action.x
                        context.players[obj.key].y = obj.action.y
                        break;
                    case "direction":
                        context.players[obj.key].direction = obj.action.direction
                }
            }
        });
        mk.SocketManager.init();
    },
    getOpponents: function(playerId) {
        var arr = [];
        for(var i in this.players) {
             if(this.players[i] && i!=playerId
                 && Math.abs(this.players[i].x - this.players[playerId].x)<300
                 && Math.abs(this.players[i].y - this.players[playerId].y)<50) {
                 arr.push(this.players[i]);
             }
        }
        return arr;
    }
};
